/***************************************************
 * Module  :
 *   Multiplexers 
 * Project :
 *   Capsense Touch sensor keybord project
 * History :
 *   May 08, 2010 - New Create
 **************************************************/

#include <avr/io.h>

int main(void)
{
	unsigned char flag;
	unsigned char pina;
	unsigned char pinb;
	unsigned char pinc;

	DDRA = 0b00000000;
	DDRB = 0b00000000;
	DDRC = 0b00000000;
	DDRD = 0b00111111;

	while(1){
		flag = PIND;
		flag &= 0b11000000;
		switch(flag){
		case 0b00000000:
			PORTD = PINA & 0b00111111;
			break;
		case 0b01000000:
			PORTD = PINB & 0b00111111;
			break;
		case 0b10000000:
			PORTD = PINC & 0b00111111;
			break;
		case 0b11000000:
			// A6 -> D0
			// A7 -> D1
			// B6 -> D2
			// B7 -> D3
			// C6 -> D4
			// C7 -> D5
			
			pina = PINA; // XX000000 -> 00000000XX
			pina >>= 6;
			pina &= 0b00000011;

			pinb = PINB; // XX000000 -> 000000XX00
			pinb >>= 4;
			pinb &= 0b00001100;

			pinc = PINC; // XX000000 -> 0000XX0000
			pinc >>= 2;
			pinc &= 0b00110000;

			PORTD = pina | pinb | pinc;
			break;
		}
	}
}
